package us.versus.them.hmap;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

class LoadableSprite extends Sprite {
	var hasBeenLoaded : Bool;
	var images : Hash< DisplayObject >;
	var loaded : Void -> Void;
	var bitmap : Bitmap;

	public function new( 
		  ?dir : String
		, ?sprite : String
		, ?directions : Array< String > 
		, ?frames : Array< String >
		, ?format : String
	) {
		super();
		this.hasBeenLoaded = false;
		if ( null != dir ) {
			this.load( dir, sprite, directions, frames, format );
		}
	}

	public function linkedCopy() {
		return this.link( new LoadableSprite() );
	}

	public function link( other:LoadableSprite ) {
		if ( this.hasBeenLoaded ) {
			other.images = this.images;
			other.hasBeenLoaded = true;
			other.loaded();
		} else {
			trace( 'ouch... not loaded... boo!' );
		}
	}

	public function load(
		dir : String
		, sprite : String
		, directions : Array< String > 
		, frames : Array< String >
		, ?format : String
	) {
		( new MultipleLoader( "demo." + sprite ) ).load( 
			this.spriteUris( 
				dir 
				, sprite
				, directions
				, frames 
				, format
			)
			, onComplete 
		);
	}

	public function onComplete( name : String, images : Hash< DisplayObject > ) {
		var ereg : EReg = ~/^[^_]*_(..)([0-9]).gif$/;
		this.images = new Hash < DisplayObject >();
		for ( key in images.keys() ) {
			if ( !ereg.match( key ) ) {
				trace( "koo " + key );
			} else {
				var dir   = ereg.matched( 1 );
				var frame = ereg.matched( 2 );
				var name = dir + "." + frame;
				this.images.set( name , images.get( key ) );
			}
		}
		this.hasBeenLoaded = true;
		this.loaded();
	}

	public function useFrame( dir : String, frame : String ) {
		if ( null != this.images ) {
			this.useImage( this.images.get( dir + "." + frame ) );
		}
	}


	public function useImage( image : DisplayObject ) {
		if ( null == image ) {
			//trace( 'she blinded me with science!' );
		} else {
			if ( null == this.bitmap ) {
				this.bitmap = new Bitmap( this.newBitmapData( image ) );
				super.addChild( bitmap );
			} else {
				// FIXME: there must be a better way...
				//this.bitmap.bitmapData = this.newBitmapData( image );
				this.bitmap.bitmapData.fillRect( 
					new flash.geom.Rectangle( 0, 0, image.width, image.height )
					, 0x0 
				);
			}
			this.bitmap.bitmapData.draw( image );
		}
	}

	public function newBitmapData( image : DisplayObject ) {
		return new BitmapData( 
			Math.floor( image.width )
			, Math.floor( image.height ) 
			, true
			, 0x0
		);
	}

	public function spriteUris( 
			dir : String
			, sprite : String
			, directions : Array< String > 
			, frames : Array< String >
			, ?format : String
			) {
		var imageNames : Array< String > = new Array< String >();
		if ( null == format ) {
			format = "gif";
		}
		for ( direction in directions ) {
			for ( frame in frames ) {
				imageNames.push( 
					dir + "/" + sprite + "_" + direction + "" + frame + "." + format 
				);
			}
		}
		return imageNames;
	}
}
